﻿#include "AbstractScrollAreaPropertyEditEntry.h"
#include "EnumEditEntry.h"
#include <QAbstractScrollArea>

#include "ObjectPropertyEditEntryFactory.h"
REGISTER_PROPERTY_EDIT_ENTRY(AbstractScrollArea)

struct AbstractScrollAreaPropertyEditEntry::AbstractScrollAreaEditEntryPrivate {
    QPointer<QAbstractScrollArea> scrollArea;
    QPointer<EnumEditEntry> entryVerticalScrollBarPolicy;
    QPointer<EnumEditEntry> entryHorizontalScrollBarPolicy;
    QPointer<EnumEditEntry> entrySizeAdjustPolicy;

};

AbstractScrollAreaPropertyEditEntry::~AbstractScrollAreaPropertyEditEntry() { }

AbstractScrollAreaPropertyEditEntry::AbstractScrollAreaPropertyEditEntry(QWidget *parent) : PropertyEditEntry {parent} {
    p.reset(new AbstractScrollAreaEditEntryPrivate);
    initUi();
    retranslateUi();
}

AbstractScrollAreaPropertyEditEntry::AbstractScrollAreaPropertyEditEntry(void *object, QWidget *parent) : PropertyEditEntry {parent} {
    p.reset(new AbstractScrollAreaEditEntryPrivate);
    initUi();
    retranslateUi();
    setObject(object);
}

void AbstractScrollAreaPropertyEditEntry::setObject(void *object) {
    const auto scrollArea = static_cast<QAbstractScrollArea*>(object);
    Q_ASSERT(Q_LIKELY(scrollArea));
    p->scrollArea = scrollArea;
}

void AbstractScrollAreaPropertyEditEntry::onTick() {
    if(!p->scrollArea || !isVisible()) {
        return ;
    }

    p->entryVerticalScrollBarPolicy->setValue(p->scrollArea->verticalScrollBarPolicy());
    p->entryHorizontalScrollBarPolicy->setValue(p->scrollArea->horizontalScrollBarPolicy());
    p->entrySizeAdjustPolicy->setValue(p->scrollArea->sizeAdjustPolicy());
}

void AbstractScrollAreaPropertyEditEntry::initUi() {
    p->entryVerticalScrollBarPolicy = new EnumEditEntry;
    p->entryVerticalScrollBarPolicy->setMetaEnum(QMetaEnum::fromType<Qt::ScrollBarPolicy>());
    appendChildEntry(p->entryVerticalScrollBarPolicy);

    p->entryHorizontalScrollBarPolicy = new EnumEditEntry;
    p->entryHorizontalScrollBarPolicy->setMetaEnum(QMetaEnum::fromType<Qt::ScrollBarPolicy>());
    appendChildEntry(p->entryHorizontalScrollBarPolicy);

    p->entrySizeAdjustPolicy = new EnumEditEntry;
    p->entryHorizontalScrollBarPolicy->setMetaEnum(QMetaEnum::fromType<QAbstractScrollArea::SizeAdjustPolicy>());
    appendChildEntry(p->entrySizeAdjustPolicy);
}

void AbstractScrollAreaPropertyEditEntry::retranslateUi() {
    setTitle("QAbstractScrollArea");
    p->entryVerticalScrollBarPolicy->setTitle(tr("verticalScrollBarPolicy"));
    p->entryHorizontalScrollBarPolicy->setTitle(tr("horizontalScrollBarPolicy"));
    p->entrySizeAdjustPolicy->setTitle(tr("sizeAdjustPolicy"));
}


